package com.engine.salary.biz;

import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.mapper.salarysob.SalarySobItemMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;
import weaver.conn.mybatis.MyBatisFactory;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class SalarySobItemBiz {

    public List<SalarySobItemPO> listAll() {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            return mapper.listAll();
        } finally {
            sqlSession.close();
        }
    }

    public List<SalarySobItemPO> listSome(SalarySobItemPO build) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            return mapper.listSome(build);
        } finally {
            sqlSession.close();
        }
    }


    public void batchInsert(Collection<SalarySobItemPO> salarySobItemPOS) {
        if (CollectionUtils.isEmpty(salarySobItemPOS)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            List<List<SalarySobItemPO>> partition = Lists.partition((List) salarySobItemPOS, 100);
            partition.forEach(mapper::batchInsert);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


    public void deleteBySalarySobIds(Collection<Long> salarySobIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            mapper.deleteBySalarySobIds(salarySobIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public void deleteItemShowBySalarySobId(Collection<Long> salarySobIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            mapper.deleteItemShowBySalarySobId(salarySobIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }



    public List<SalarySobItemPO> listBySalarySobIdAndGroupId(Long salarySobId, Collection<Long> salarySobItemGroupIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            return mapper.listBySalarySobIdAndGroupId(salarySobId, salarySobItemGroupIds);
        } finally {
            sqlSession.close();
        }
    }


    public void deleteByIds(List<Long> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return;
        }

        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            mapper.deleteByIds(ids);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public void deleteByGroupIds(List<Long> needDeleteGroupIds) {
        if (CollectionUtils.isEmpty(needDeleteGroupIds)) {
            return;
        }

        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            mapper.deleteByGroupIds(needDeleteGroupIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

    }


    public void update(SalarySobItemPO salarySobItemPO) {
        if (Objects.isNull(salarySobItemPO)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobItemMapper mapper = sqlSession.getMapper(SalarySobItemMapper.class);
            mapper.updateIgnoreNull(salarySobItemPO);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }
}
